/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.businesslogic;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import ticketmaster.businessobject.User;
import ticketmaster.businessobject.LoginUser;
import ticketmaster.dao.DAOFactory;
import ticketmaster.dao.PremiumCustomerDAO;
import ticketmaster.dataobject.PremiumCustomer;
import ticketmaster.presentation.customer.PremiumCustomerPanel;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Wei Liu
 */
public class PremiumCustomerController
{
    public PremiumCustomer getPremiumCustomer(User customer)
    {
        PremiumCustomerDAO dao = DAOFactory.getInstance().getPremiumCustomerDAO();
        PremiumCustomer pc = new PremiumCustomer();
        pc.setCustomerID(customer.getUserID());
        pc.setCancelled(false);
        return dao.findPremiumCustomer(pc);
    }

    public boolean updatePremiumCustomer(PremiumCustomer customer)
    {
        PremiumCustomerDAO dao = DAOFactory.getInstance().getPremiumCustomerDAO();
        return dao.updatePremiumCustomer(customer);
    }

    public void upgradRegularToPremium()
    {
        createNewPremium();
    }

    public void createNewPremium()
    {
        int userID = LoginUser.getUser().getUserID();
        PremiumCustomer premiumCustomer = new PremiumCustomer();
        premiumCustomer.setCustomerID(userID);
        premiumCustomer.setPoints(0);
        premiumCustomer.setRegistrationDate(DateTimeHelper.getCurrentDateTime());
        Calendar calendar = DateTimeHelper.getCurrentDateTimeCalendar();
        calendar.add(Calendar.YEAR, 1);
        premiumCustomer.setExpireDate(new Timestamp(calendar.getTimeInMillis()));
        DAOFactory.getInstance().getPremiumCustomerDAO().insertPremiumCustomer(premiumCustomer);
    }

    public void postponePremiumExpireDate()
    {
        int userID = LoginUser.getUser().getUserID();
        PremiumCustomer premiumCustomer = new PremiumCustomer();
        premiumCustomer.setCustomerID(userID);
        DAOFactory.getInstance().getPremiumCustomerDAO().getPremiumCustomersDO(premiumCustomer);
        Timestamp expireDate = premiumCustomer.getExpireDate();
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(expireDate.getTime());
        calendar.add(Calendar.YEAR, 1);
        premiumCustomer.setExpireDate(new Timestamp(calendar.getTimeInMillis()));
        DAOFactory.getInstance().getPremiumCustomerDAO().updatePremiumCustomer(premiumCustomer);
    }
}
